<template>
	<view class="profile-page">
		<button v-if="!memberLevel" class="phoneNumber_btn" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber"></button>
		<!-- 会员头部 -->
		<view class="header">

			<view class="vip_code" @click="toQrCode">
				<u-icon name="grid" color="#fff"></u-icon>会员码
			</view>
			<view class="users">
				<view class="avatar">
					<image :src="avatar?avatar:'https://img-cdn.duobashuzi.com/xianggui/img/default-avatar.png'" class="avatar-img" mode="aspectFill" />
					
				</view>
				<view class="user-info">
					<text class="nickname" v-if="!avatar||!nickname" @click="getInfos">点击显示微信头像</text>
					<view class="membership" v-else>
					  <view class="member-icon">{{nickname}}</view>
					  <text class="member-benefits">{{memberLevel==0?'会员中心':'LV.'+memberLevel+'>'}}</text>
					</view>
					
					
				</view>
			</view>

			<!-- 注册 -->
			<view class="register_box" @click="toVip">
				<view class="vip_info" style="display: flex; align-items: center;">
					<image style="width: 20px; margin-right: 5px;" src="https://img-cdn.duobashuzi.com/xiangguijia/zuan.png" mode="widthFix"></image>
					{{memberLevel?'康源生活':'成为会员，享3大会员专享权益'}}
				</view>
				<view class="vip_level">
					{{memberLevel?info.member_level_text:'立即注册'}}
					<u-icon name="arrow-right" color="#e3d2cb"></u-icon>
				</view>
			</view>
		</view>
		<view class="login_box" v-if="!memberLevel">
			<view class="">
				登录手机号，订单管理更轻松，优惠信息不错过
			</view>
			<button size="mini" class="login-button" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">
				登录
			</button>
		</view>
		<!-- 数据统计 -->
		<view class="ss">
			<button v-if="!memberLevel" class="phoneNumber_btn2" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber"></button>
			<view class="stats">
				<view class="stat-item" v-for="(item, index) in stats" :key="index" @click="memberLevel?toMenuUrl(item.url,0,item.value):''" :num="item.value">
					<view class="stat-value" v-if="memberLevel">{{ item.value }}</view>
					<view class="stat-value" v-else>-</view>
					<view class="stat-label">{{ item.label }}</view>
				</view>
			</view>
			<!-- 我的订单 -->
			<view class="orders">
				<view class="orders-header">
					<text>我的订单</text>
					<view class="view-all" @click="viewAllOrders">查看全部订单 <u-icon name="arrow-right" color="#999"
							size="12px"></u-icon></view>
				</view>
				<view class="orders-list">
					<view class="order-item" @click="toOrder(item)" v-for="(item, index) in orders" :key="index">
						<view class="num_icon" v-if="item.num!=0">
							{{item.num}}
						</view>
						<image class="order_icon" :src="item.icon" mode="widthFix"></image>
						<view>{{ item.text }}</view>
					</view>
				</view>
			</view>
			<!-- 其他菜单 -->
			<view class="menu">
				<view class="menu-item" v-for="(item, index) in menus" :key="index" @click="toMenuUrl(item.url,item.switchs)">
					<view class="menu_name">
						<image :src="item.icon"></image>
						<view class="">
							{{item.name}}
						</view>
					</view>
					<u-icon name="arrow-right" color="#999" size="14px"></u-icon>
				</view>
				
				<view class="menu-item" v-if="ischeckAgentStatus == 1"  @click="toMenuUrl('/package/agent/browse',0)">
					<view class="menu_name">
						<image src="https://img-cdn.duobashuzi.com/xiangguijia/my-dali.png"></image>
						<view class="">分销中心</view>
					</view>
					<u-icon name="arrow-right" color="#999" size="14px"></u-icon>
				</view>
			</view>
		</view>

		<view class="cart_manager" @click="onfhsy">
			<view class="cart_managerleft">
				<image class="cart_managerimg" src="https://img-cdn.duobashuzi.com/xianggui/img/dianpu.png"
					mode="widthFix"></image>
				<text>康源生活</text>
			</view>
			<view class="diFlex dianp">
				<text>进入店铺</text>
				<u-icon name="arrow-right" color="#999" size="14px"></u-icon>
			</view>
		</view>
		<privacy-popup :privacyAuthPopup.sync="privacyAuthPopup"></privacy-popup>
		<user-popup @updateInfo="updateInfo" :userUp.sync="userUp" :avatar.sync="avatar" :nickname.sync="nickname"></user-popup>
		<view id="recommend"  class="recommendli">
			<u-divider text="更多个性化推荐"></u-divider>
			<sheet-recommend :isType.sync="isType" ref="getXgjgoodlist"></sheet-recommend>
		</view>
	</view>
</template>


<script>
	import PrivacyPopup from '../../components/privacy-popup/privacy-popup.vue';
	import UserPopup from '../../components/userInfo-popup/user-popap.vue'
	import sheetRecommend from "@/components/sheet/sheetRecommend.vue"
	import {getPhoneNumber,getUserInfo,menusNum,checkAgentStatus} from '@/api/my.js'

	export default {
		data() {
			return {
				isType:1,
				avatar:'',
				nickname:'',
				userInfo: {},
				stats: [{
						label: "余额",
						value: "0.00",
						url:'/package/datas/money'
					},
					{
						label: "健康豆",
						value: "0",
						url:'/package/datas/points'
					},
					// {
					// 	label: "卡",
					// 	value: "0",
					// 	url:'/package/datas/card'
					// },
					// {
					// 	label: "优惠券",
					// 	value: "0",
					// 	url:'/package/datas/yhq'
					// }
				],
				// orders: ["待付款", "待发货", "待收货", "待评价", "退换/售后"],
				orders: [{
					text: '待付款',
					id: 2,
					icon: 'https://img-cdn.duobashuzi.com/xiangguijia/order1.png',
					num:0
				}, {
					text: '待发货',
					id: 3,
					icon: 'https://img-cdn.duobashuzi.com/xiangguijia/order2.png',
					num:0
				}, {
					text: '待收货',
					id: 4,
					icon: 'https://img-cdn.duobashuzi.com/xiangguijia/order3.png',
					num:0
				}, {
					text: '待评价',
					id: 5,
					icon: 'https://img-cdn.duobashuzi.com/xiangguijia/order4.png',
					num:0
				}, {
					text: '退换/售后',
					id: 6,
					icon: 'https://img-cdn.duobashuzi.com/xiangguijia/order5.png',
					num:0
				}],
				menus: [
					{
						name:'购物车',
						icon:'https://img-cdn.duobashuzi.com/xiangguijia/my-cart.png',
						url:'/pages/cart/index',
						switchs:1
					},{
						name:'任务中心',
						icon:'https://img-cdn.duobashuzi.com/xiangguijia/my-task.png',
						url:'/package/task/index',
						switchs:0
					},{
						name:'返现',
						icon:'https://img-cdn.duobashuzi.com/xiangguijia/my-fan.png',
						url:'/package/datas/fanxian',
						switchs:0
					},{
						name:'赠品',
						icon:'https://img-cdn.duobashuzi.com/xiangguijia/my-gift.png',
						url:'/package/datas/gift',
						switchs:0
					},{
						name:'收货地址',
						icon:'https://img-cdn.duobashuzi.com/xiangguijia/my-address.png',
						url:'/package/address/index',
						switchs:0
					},{
						name:'个人信息',
						icon:'https://img-cdn.duobashuzi.com/xiangguijia/my-info.png',
						url:'/package/userInfo/index',
						switchs:0
					}],
				privacyAuthPopup: false,
				userUp: false,
				memberLevel:0,
				ischeckAgentStatus:0,
				info:{}
			};
		},
	
		components: {
			PrivacyPopup,
			UserPopup,
			sheetRecommend
		},
		
		created() {
			
			this.$nextTick(()=>{
				this.$refs.getXgjgoodlist.getRecommendlist();
			})
		},
		onShow() {
			this.$nextTick(()=>{
				this.$refs.getXgjgoodlist.getRecommendlist();
			})
			this.getcheckAgentStatus();
			this.init();
			this.getInfo();
		},
	
		methods: {
			async init(){
				let memberLevel=  await this.getInfo();
				if(memberLevel!=0){
					this.getMenusNum()
				}
			},
			getcheckAgentStatus(){
				checkAgentStatus({}).then(res=>{
					this.ischeckAgentStatus =  res.data;
					console.log(res.data)
				})
			},
			updateInfo(e){
				this.getInfo();
			},
			getMenusNum(){
				menusNum({}).then(res=>{
					const arr = [res.data.dzfCount,res.data.dfhCount,res.data.dshCount,res.data.dpjCount,res.data.tkShCount]
					this.orders = this.orders.map((v,i)=>{
						v.num = arr[i]
						return v
					})
				})
			},
			toOrder(item){
				if(item.id==6){
					uni.navigateTo({
						url:'/package/orderList/refund'
					})
				}else{
					uni.navigateTo({
						url:'/package/orderList/index?id='+item.id
					})
				}
			},
			toQrCode(){
				if(!this.avatar||!this.nickname){
					this.userUp = true
				}else{
					uni.navigateTo({
						url:'/package/qrcode/index'
					})
				}
				
			},
			// 跳转页面
			toMenuUrl(url,switchs=0,val=0){
				if(url&&switchs==1){
					uni.switchTab({
						url
					})
				}else if(url&&switchs==0){
					
					uni.navigateTo({
						url:url+'?val='+val
					})
				}
				
			},
			onfhsy(){
				uni.switchTab({
					url:'/pages/active/index'
				})
			},
			// 获取用户头像和昵称
			getInfos(){
				this.userUp = true
			},
			async getInfo(){
				getUserInfo({}).then(res=>{
					console.log(res)
					this.memberLevel = res.data.member_level
					this.avatar = res.data.head_pic
					this.nickname = res.data.nick_name
					this.info = res.data
					uni.setStorageSync('info',res.data)
					uni.setStorageSync('memberLevel',res.data.member_level)
					uni.setStorageSync('mobile',res.data.bind_mobile)
					if(this.memberLevel!=0){
						// const datasarr = [res.data.balance,res.data.health_point,res.data.card,res.data.coupon]
						const datasarr = [res.data.use_points,res.data.health_point]
						console.log(datasarr)
						this.stats = this.stats.map((item,index)=>{
							item.value = datasarr[index]
							return item
						})
					}
					Promise.resolve(res.data.member_level)
					
				})
			},
			// 前往会员中心
			toVip(){
				uni.navigateTo({
					url:'/package/memberCenter/index?level='+this.memberLevel
				})
			},
			privacy() {
				let that = this;
				if (wx.getPrivacySetting) {
					wx.getPrivacySetting({
						success: (res) => {
							console.log(res);
							if (res.needAuthorization) {

								this.popUp();
							} else {
								// 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用隐私接口
							}
						},
						fail: () => {},
						complete: () => {}
					});
				} else {
					this.triggerEvent('agree');
				}
			},
			//打开
			popUp() {
				this.privacyAuthPopup = true;
			},
			// 微信手机号授权
			onGetPhoneNumber(e) {
				// this.privacy()
				if (e.detail.encryptedData) {
					// 使用微信解密接口（需配合后端）
					console.log("微信手机号登录成功:", e.detail);
			
					if(e.detail.errMsg==='getPhoneNumber:ok'){
						const data = {
							mobile:e.detail.code
						}
						getPhoneNumber(data).then(res=>{
							uni.showToast({
								title:res.msg,
								icon:'none'
							})
							this.memberLevel = res.member_level
							this.getInfo()
						}).catch(err=>{
							uni.showToast({
								title:err.data,
								icon:'none'
							})
						})
					}
					
				} else {}
			},
			// 查看全部订单
			viewAllOrders(){
				uni.navigateTo({
					url:'/package/orderList/index'
				})
			},

		}
	};
</script>

<style lang="scss" scoped>
	@import 'my.scss';

	.login_btn {
		font-size: 16px;
		color: #333;
	}
	.menu_name{
		display: flex;
		align-items: center;
		image{
			width: 20px;
			height: 20px;
			margin-right: 8px;
		}
	}
	.ss{
		position: relative;
		.phoneNumber_btn2{
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			opacity: 0;
		}
	}
</style>